package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.AdminSupTypeDao;
import ex.com.jm.food.entity.SubType;
import ex.com.jm.food.entity.SupType;

/**
 * 
 * @author zhaojw
 *
 */
public class AdminSupTypeDaoImpl extends HibernateDaoSupport implements
		AdminSupTypeDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<SupType> getSupTypes(final int initPage,final int pageRow,final String value) {
		// TODO Auto-generated method stub
		Object supTypes=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="select new ex.com.jm.food.entity.dto.SupTypeDTO(sup.supId,sup.supName,sup.created) from SupType sup where 1=1 ";
				if(value!=null&&!value.equals("")){
					hql+=" and sup.supName like '%"+value+"%'";
				}
				hql+=" order by sup.created desc ";
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<SupType>) supTypes;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int supTypeCount(final String value) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from SupType sup where 1=1 ";
				if(value!=null&&!value.equals("")){
					hql+=" and sup.supName like '%"+value+"%'";
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@Override
	public Object saveSupType(SupType supType) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(supType);
	}

	@Override
	public void updateSupType(SupType supType) {
		// TODO Auto-generated method stub
           this.getHibernateTemplate().update(supType);
	}

	@Override
	public SupType editSupType(Integer id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(SupType.class, id);
	}

	@Override
	public void delSupType(Integer id) {
		// TODO Auto-generated method stub
           this.getHibernateTemplate().delete(this.getHibernateTemplate().get(SupType.class, id));
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public SupType checkName(final String name) {
		// TODO Auto-generated method stub
		SupType supType=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("from SupType where supName="+name);
				return query.uniqueResult();
			}
		});
		return supType;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<SubType> getSubTypes(final int initPage,final int pageRow,final String value,
			final int supId) {
		// TODO Auto-generated method stub
		Object suptype=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from SubType sub where supId="+supId+"";
				if(value!=null&&!value.equals("")){
					hql+=" and sub.subName like '%"+value+"%'";
				}
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<SubType>) suptype;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int subCount(final String value,final int supId) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String hql="select count(*) from SubType sub where supId="+supId+"";
				if(value!=null&&!value.equals("")){
					hql+=" and sub.subName like '%"+value+"%'";
				}
				Query query=session.createQuery(hql).setFirstResult(0).setMaxResults(1);
				return query.list().get(0);
			}
		});
		return count;
	}

}
